<template>
    <div class="publishedJobList">
        <h1>已发布职位列表</h1>
        <div v-for="job, index in jobList" :key="job.job_recruitment_id">
            <el-descriptions title="" direction="vertical" :column="4" border style="margin-bottom:100px">
                <el-descriptions-item label="职位名称">
                    <el-input v-model="job.job_title" disabled></el-input>
                </el-descriptions-item>
                <el-descriptions-item label="职位分类">
                    <el-input v-model="job.job_classification" disabled></el-input>
                </el-descriptions-item>
                <el-descriptions-item label="薪资">
                    <el-input v-model="job.salary" disabled></el-input>
                </el-descriptions-item>
                <el-descriptions-item label="收到简历数目">
                    <el-input v-model="receivedResumeCountList[index]" disabled></el-input>
                </el-descriptions-item>
                <el-descriptions-item label="操作">
                    <el-button type="primary" @click="detail(job.job_recruitment_id)">查看职位详情</el-button>
                    <el-button type="primary" @click="goResumeList(job.job_recruitment_id)"
                        v-if="isReceivedResumeList[index]">查看收到的简历</el-button>
                </el-descriptions-item>
            </el-descriptions>
        </div>
        <div style="margin-top:10px;margin-left:-250px;margin-bottom:150px;">
            <div class="block">
                <el-pagination :current-page="currentPage" :page-size="pageSize" layout="total,  prev, pager, next, jumper"
                    :total="total" @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            isReceivedResumeList: [],
            receivedResumeCountList: [],
            enterprise_number: '',
            jobList: [],
            pageSize: 2,
            pagerCount: 0,
            total: 0,
            currentPage: 1,
        }
    },
    methods: {
        goResumeList(id) {
            window.localStorage.setItem("job_recruitment_id", id);
            this.$router.push({ name: 'enterprise_deliver_resume_list', params: { job_recruitment_id: id } }, () => { }, () => { });
        },
        getJobList() {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/job_recruitment/get_list',
                params: {
                    enterprise_number: that.enterprise_number,
                    page: 1,
                    size: that.pageSize
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.jobList = response.data.result.list;
                    that.total = Number.parseInt(response.data.result.count);
                    that.pagerCount = that.total % that.pageSize;
                    that.jobList.forEach(element => {
                        that.getReceivedResumeCount(element.job_recruitment_id);
                    });
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        getReceivedResumeCount(id) {
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/deliver_resume/get_list',
                params: {
                    enterprise_number: that.enterprise_number,
                    job_recruitment_id: id,
                    page: that.currentPage,
                    size: that.pageSize
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    if (response.data.result.count) {
                        that.receivedResumeCountList.push(response.data.result.count);
                        that.isReceivedResumeList.push(true);
                        return;
                    }
                    that.receivedResumeCountList.push(0);
                    that.isReceivedResumeList.push(false);
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
        detail(id) {
            this.$router.push({ name: 'enterprise_job_detail', params: { job_recruitment_id: id } }, () => { }, () => { });
        },
        handleCurrentChange(val) {
            this.isReceivedResumeList = [];
            this.receivedResumeCountList = [];
            if (!window.localStorage.getItem("token")) {
                this.$router.push({ path: '/login' }, () => { }, () => { });
                return;
            }
            let that = this;
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/api/job_recruitment/get_list',
                params: {
                    enterprise_number: window.localStorage.getItem("enterprise_number"),
                    page: val,
                    size: that.pageSize
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.jobList = response.data.result.list;
                    that.total = Number.parseInt(response.data.result.count);
                    that.pagerCount = that.total % that.pageSize;
                    that.jobList.forEach(element => {
                        that.getReceivedResumeCount(element.job_recruitment_id);
                    });
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
    },
    created: function () {
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/get_obj',
            params: {
                user_id: window.localStorage.getItem("user_id")
            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                if (!response.data.result) {
                    that.$message.error("您还没有完善企业信息!请去完善企业信息!!!");
                    that.$router.push({ name: 'enterprise_information' }, () => { }, () => { });
                }
                that.enterprise_number = response.data.result.obj.enterprise_number;
                window.localStorage.setItem("enterprise_number", response.data.result.obj.enterprise_number);
                that.getJobList();
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    }
}
</script>
<style lang="less" scoped>
.publishedJobList {
    margin-left: 450px;
    margin-top: -500px;
    margin-bottom: 10px;
}
</style>
